package com.redis.set;

import java.util.Iterator;
import java.util.Set;

import redis.clients.jedis.Jedis;

public class RedisSet {
	public static void main(String[] args) {
		diffSet();
	}
	
	/**
	 * 两个集合的差集
	 */
	public static void diffSet() {
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.sadd("java 1 class", "zhangsan", "lisi", "wangwu", "zhaoliu");
		jedis.sadd("game 1 class", "wangwu", "lisi", "xiaocui", "xiaohua");
		
		Set<String> set = jedis.sdiff("java 1 class", "game 1 class");
		Iterator<String> it = set.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
		jedis.close();
	}
	
	/**
	 * 两个集合的并集
	 */
	public static void unionSet() {
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.sadd("java 1 class", "zhangsan", "lisi", "wangwu", "zhaoliu");
		jedis.sadd("game 1 class", "wangwu", "lisi", "xiaocui", "xiaohua");
		
		Set<String> set = jedis.sunion("java 1 class", "game 1 class");
		Iterator<String> it = set.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
		jedis.close();
	}
	
	/**
	 * 两个集合的交集
	 */
	public static void interSet() {
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.sadd("java 1 class", "zhangsan", "lisi", "wangwu", "zhaoliu");
		jedis.sadd("game 1 class", "wangwu", "lisi", "xiaocui", "xiaohua");
		
		Set<String> set = jedis.sinter("java 1 class", "game 1 class");
		Iterator<String> it = set.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
		jedis.close();
	}
	
	/**
	 * 删除Set集合中的元素
	 */
	public static void deleteMember() {
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		System.out.println(jedis.srem("user", "male"));
		jedis.close();
	}
	
	/**
	 * Set集合的元素个数
	 */
	public static void setLength() {
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		System.out.println(jedis.scard("user"));
		jedis.close();
	}
	
	/**
	 * 判读某个元素是否是Set集合的元素
	 */
	public static void isSetMember() {
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		System.out.println(jedis.sismember("user", "22"));
		jedis.close();
	}
	
	/**
	 * 向Set集合中添加元素
	 */
	public static void addToSet() {
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		jedis.sadd("user", "zhangsan");
		jedis.sadd("user", "22", "male");
		System.out.println(jedis.smembers("user"));
		jedis.close();
	}
	
}
